import sys
import math
import typing
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets

class ComeToScene(QGraphicsItem):
    def __init__(self,scene):
        super(ComeToScene, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()
        self.pix = QPixmap(self.root+"/res/colorful_spiral.png")
        self.rotation = 0  #旋转角度
        self.rotatespeed = 10 #旋转速度

        self.opacity = 1



        self.scene = scene
        self.scene.addItem(self)

    def boundingRect(self):
        return QRectF(-self.pix.width() * 0.5, -self.pix.height() * 0.5, self.pix.width(), self.pix.height())

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):
        painter.setOpacity(self.opacity)
        painter.drawPixmap(-self.pix.width()*0.5,-self.pix.height()*0.5,self.pix)
        # painter.resetTransform()
        # painter.translate(self.pos().x() + self.red_pix.width()*0.5,self.pos().y() + self.red_pix.height()*0.5)
        # painter.rotate(90)
        # painter.drawPixmap(-self.blue_pix.width()*0.5,-self.blue_pix.height()*0.5,self.blue_pix)
        # painter.drawPixmap(-self.pix.width()*0.5,-self.pix.height()*0.5,self.pix)

        
    def setPos(self, pos: typing.Union[QPointF, QPoint]):
        super(ComeToScene, self).setPos(pos)

    def updateAnimation(self):
        self.opacity -=0.02    #提高透明度

        self.rotatespeed += 5   #加速旋转
        self.rotation = (self.rotation+self.rotatespeed)%360
        self.setRotation(self.rotation)
